package com.guigu.youxuan.controller;


import com.guigu.youxuan.common.utils.PageUtils;

import com.guigu.youxuan.pojo.BackMenu;
import com.guigu.youxuan.pojo.BackUser;
import com.guigu.youxuan.pojo.GoodsCategory;

import com.guigu.youxuan.service.IGoodsCategoryService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liweixin
 * @since 2024-05-15
 */
@CrossOrigin
@RestController
@RequestMapping("/goodsCategory")
public class GoodsCategoryController {
    @Autowired
    private IGoodsCategoryService GoodsCategoryService;


    /**
     * 分层的商品类型
     * @return
     */
    @RequestMapping("/queryGoodsTypeListLevel")
    public List<GoodsCategory> queryGoodsTypeListLevel() {
        List<GoodsCategory> list = GoodsCategoryService.queryGoodsCategoryList();

        //分层
        List<GoodsCategory> finalList = new ArrayList<>();
        for(GoodsCategory goodsCategory : list){
            if(goodsCategory.getLevel() == 1) {
                List<GoodsCategory> children = new ArrayList<>();
                for (GoodsCategory subGoodsCategory : list) {
                    if (subGoodsCategory.getLevel() == 2 && subGoodsCategory.getParentId().equals(goodsCategory.getId())) {
                        List<GoodsCategory> grandChildren = new ArrayList<>();
                        for (GoodsCategory grandsubGoodsCategory : list) {
                            if (grandsubGoodsCategory.getLevel() == 3 && grandsubGoodsCategory.getParentId().equals(subGoodsCategory.getId())) {
                                grandChildren.add(grandsubGoodsCategory);
                            }
                        }
                        subGoodsCategory.setChildren(grandChildren);
                        children.add(subGoodsCategory);
                    }
                }

                goodsCategory.setChildren(children);

                finalList.add(goodsCategory);
            }
        }
        return finalList;
    }

    /**
     * 删除商品类型
     * @param goodsCategory
     * @return
     */
    @RequestMapping("/delGoodsCategory")
    public String delGoodsCategory(@RequestBody GoodsCategory goodsCategory) {
        GoodsCategoryService.delGoodsCategory(goodsCategory);
        return "删除成功！";
    }

    /**
     * 批量删除商品类型
     * @param goodsCategoryList
     * @return
     */
    @RequestMapping("/delsGoodsCategory")
    public String delsGoodsCategory(@RequestBody List<GoodsCategory> goodsCategoryList) {
        GoodsCategoryService.delsGoodsCategory(goodsCategoryList);
        return "删除成功！";
    }

    /**
     * 修改商品类型
     * @param goodsCategory
     * @return
     */
    @RequestMapping("/updateGoodsCategory")
    public String updateGoodsCategory(@RequestBody GoodsCategory goodsCategory) {
        GoodsCategoryService.updateGoodsCategory(goodsCategory);
        return "修改商品类型成功！";
    }

    /**
     * 商品类型不分页查询
     * @return
     */
    @RequestMapping("/queryGoodsCategoryList")
    public List<GoodsCategory> queryGoodsCategoryList() {
        List<GoodsCategory> goodsCategoryList = GoodsCategoryService.queryGoodsCategoryList();
        return goodsCategoryList;
    }

    /**
     * 修改商品类型的回显查询
     * @param goodsTypeId
     * @return
     */
    @RequestMapping("/queryGoodsTypeById/{goodsTypeId}")
    public GoodsCategory queryGoodsTypeById(@PathVariable Long goodsTypeId ) {
        GoodsCategory goodsCategory = GoodsCategoryService.queryGoodsTypeById(goodsTypeId);
        return goodsCategory;
    }

    /**
     * 新增商品类型
     * @param goodsCategory
     * @return
     */
    @RequestMapping("/addGoodsType")
    public String addGoodsType(@RequestBody GoodsCategory goodsCategory) {
        GoodsCategoryService.addGoodsType(goodsCategory);
        return "新增商品类型成功！";
    }

    /**
     * 图标上传
     * @return
     */
    @RequestMapping("/avatarImgUpload")
    public String avatarImgUpload(MultipartFile file) {

        //获取源文件名(包含后缀 1.jpg)
        String originalFilename = file.getOriginalFilename();
        //截取后缀名
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        //生成唯一文件名
        String newFileName = UUID.randomUUID().toString()+suffix;
        //该文件要放在指定目录下
        File dir = new File("D:/myimgs/Goods");
        if(!dir.exists())
            dir.mkdir();
        //目标文件对象
        File destFile = new File(dir,newFileName);

        //文件写入
        try {
            file.transferTo(destFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "http://localhost:10001/images/Goods/"+newFileName;
    }

    /**
     * 商品类型禁用/启用
     * @param goodsCategory
     * @return
     */
    @RequestMapping("/switchStatus")
    public String switchStatus(@RequestBody GoodsCategory goodsCategory) {
        GoodsCategoryService.switchStatus(goodsCategory);
        if (goodsCategory.getStatus() == 0) {
            return "启用成功";
        }
        return "禁用成功";
    }

    /**
     * 商品类型级别查询
     * @return
     */
    @RequestMapping("/queryGoodsTypeLevel")
    public List<Integer> queryGoodsTypeLevel() {
        List<Integer> list = GoodsCategoryService.queryGoodsTypeLevel();
        return list;
    }

    /**
     * 商品类型
     * @param goodsCategory
     * @return
     */
    @RequestMapping("/getGoodsCategoryList/{currentPage}/{pageSize}")
    public PageUtils<GoodsCategory> getGoodsCategoryList(@RequestBody GoodsCategory goodsCategory,
                                                         @PathVariable int currentPage,
                                                         @PathVariable int pageSize) {
        PageUtils<GoodsCategory> page = GoodsCategoryService.getGoodsCategoryList(goodsCategory,currentPage,pageSize);
        return page;
    }

    /**
     * 商品分类
     * @return
     */
    @RequestMapping("/GoodsCategoryNameList")
    @ResponseBody
    public List<GoodsCategory> GoodsCategoryNameList(){
        List<GoodsCategory> goodsCategoryList=GoodsCategoryService.GoodsCategoryNameList();
        return goodsCategoryList;
    }
}
